package com.pureut.quality.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.quality.domain.QualitySchemeConfigurationDetail;
import com.pureut.quality.domain.dto.QualitySchemeConfigurationDetailDto;
import com.pureut.quality.mapper.QualitySchemeConfigurationDetailMapper;
import com.pureut.quality.service.QualitySchemeConfigurationDetailService;
import com.pureut.system.api.domain.SysDictData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/***
 * 检验方案配置明细表实现层
 * Author:C
 * Date:2023/02/03 9:21
 * @DESC
 */
@Service
public class QualitySchemeConfigurationDetailServiceImpl extends ServiceImpl<QualitySchemeConfigurationDetailMapper, QualitySchemeConfigurationDetail> implements QualitySchemeConfigurationDetailService {

    @Resource
    QualitySchemeConfigurationDetailMapper qualitySchemeConfigurationDetailMapper;

    /**
     * 检验方案配置明细列表
     *
     * @param id
     * @return
     */
    @Override
    public List<QualitySchemeConfigurationDetailDto> getSchemeConfigurationDetailList(long id) {

        List<QualitySchemeConfigurationDetailDto> detailList = qualitySchemeConfigurationDetailMapper.getDetailList(id);

        //单位
        List<SysDictData> unitArray = DictUtils.getDictCache("sys_material_unit");
        Map<String, String> unitMap = unitArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //管控方式
        List<SysDictData> controlModeArray = DictUtils.getDictCache("quality_item_control_mode");
        Map<String, String> controlModeMap = controlModeArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        for (QualitySchemeConfigurationDetailDto entity : detailList) {
            //管控方式
            entity.setItemControlModeDict(controlModeMap.get(entity.getItemControlMode()));
            //单位
            entity.setItemUnitDict(unitMap.get(entity.getItemUnit()));
        }
        return detailList;
    }

    /**
     * 根据项目编码获取信息
     *
     * @param itemCode
     * @return
     */
    @Override
    public List<QualitySchemeConfigurationDetailDto> getViewByItemCode(String itemCode) {

        List<QualitySchemeConfigurationDetailDto> viewByItemCodeList = qualitySchemeConfigurationDetailMapper.getViewByItemCodeList(itemCode);

        List<QualitySchemeConfigurationDetailDto> collect = viewByItemCodeList.stream().filter(entity -> entity.getItemRank() == 2 || entity.getItemRank() == 3 || entity.getItemRank() == 5).collect(Collectors.toList());

        //单位
        List<SysDictData> unitArray = DictUtils.getDictCache("sys_material_unit");
        Map<String, String> unitMap = unitArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //管控方式
        List<SysDictData> controlModeArray = DictUtils.getDictCache("quality_item_control_mode");
        Map<String, String> controlModeMap = controlModeArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        for (QualitySchemeConfigurationDetailDto entity : collect) {
            //管控方式
            entity.setItemControlModeDict(controlModeMap.get(entity.getItemControlMode()));
            //单位
            entity.setItemUnitDict(unitMap.get(entity.getItemUnit()));
        }
        return collect;
    }
}
